package control;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.DAOCurso;
import model.Curso;
/**
 * Servlet implementation class CadastrarCursoServlet
 */
public class CadastrarCursoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CadastrarCursoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String nome = request.getParameter("nome");
		int ch = Integer.parseInt(request.getParameter("ch"));
		String desc = request.getParameter("desc");
		
		DAOCurso daoCu = new DAOCurso();
		String msg;
		
		try {
			daoCu.begin();
			Curso c = new Curso();
			c.setNome(nome);
			c.setCargahoraria(ch);
			c.setDescricao(desc);
			daoCu.persist(c);
			daoCu.commit();
			msg = new String ("Curso cadastrado!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			msg = new String ("Erro ao cadastrar curso!");
			e.getMessage();
		}
		
		
		
		request.setAttribute("msg", msg);
		RequestDispatcher rqd = request.getRequestDispatcher("painel.jsp");
		rqd.forward(request, response);
		
		
	}

}
